package m.jclecai.com.activity.follow;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.activity.WebHelperActivity;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.fragment.hit.HitFragment;

/**
 * Created by Dinosa on 2017/11/12.
 * <p>
 * 这里是命中榜；
 */

public class HitListActivity extends BaseActivity {

    @BindView(R.id.viewPager)
    public ViewPager mViewPager;

    @BindView(R.id.tabLayout)
    public TabLayout mTabLayout;

    private String[] mStrings = {"近7天", "近30天"};
    private ArrayList<HitFragment> mHitFragments;
    private String title;
    private String type;

    @Override
    protected int getContentView() {
        return R.layout.activity_hit_list;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("命中榜");
        leftOptionText("");
        rightOptionText("说明");
    }

    @Override
    protected void initData() {
        super.initData();

        Intent intent = getIntent();
        //命中榜，或者盈利榜
        title = intent.getStringExtra("title");
        //url
        type = intent.getStringExtra("type");

        setTitle(title);

        mHitFragments = new ArrayList<>();

        for (int i = 0; i < mStrings.length; i++) {
            HitFragment hitFragment = new HitFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("flag", i);
            bundle.putString("type", type);
            hitFragment.setArguments(bundle);
            mHitFragments.add(hitFragment);
        }


        mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mHitFragments.get(position).requestData();
            }
        });


    }

    @OnClick(R.id.tv_right_option)
    public void right() {
        Intent intent = new Intent(HitListActivity.this, WebHelperActivity.class);
        intent.putExtra("title", title + "说明");
        intent.putExtra("code", type.equals("nums") ? "InstructionsOnTheListOfHits" : "ProfitOnThelist");
        intent.putExtra("isLocal", true);
        startActivity(intent);
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        @Override
        public CharSequence getPageTitle(int position) {
            return mStrings[position];
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mHitFragments.get(position);
        }

        @Override
        public int getCount() {
            return mStrings.length;
        }
    }

    public static Intent getIntent(Context context, String title, String type) {
        Intent intent = new Intent(context, HitListActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("type", type);
        return intent;
    }
}
